$('document').ready(function() {
	$('#save-app-next').on('click',function(){
		$('#add-new-app-next').submit();
	});
	$('#add-new-app-next').bootstrapValidator({
		// container: '#messages',
		excluded : [ '' ],
		feedbackIcons : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating : 'glyphicon glyphicon-refresh'
		},
		fields : {
			
			tags : {
				validators : {
					notEmpty : {
						message : 'Ít nhất một từ khóa'
					},

				}

			},
			/*des_short : {
				validators : {
					callback: {
                        message: 'Mô tả ngắn ít nhất 50 ký tự và tối đa 200 ký tự',
                        callback: function (value, validator, $field) {
                            // Get the plain text without HTML
                            var text = tinyMCE.activeEditor.getContent({
                                format: 'text'
                            });

                            return text.length <= 200 && text.length >= 50;
                        }
                    }
				}

			},*/
			description : {
				validators : {
					callback: {
                        message: 'Mô tả ít nhất 50 ký tự',
                        callback: function (value, validator, $field) {
                            // Get the plain text without HTML
                            var text = tinyMCE.activeEditor.getContent({
                                format: 'text'
                            });

                            return text.length >=50;
                        }
                    }
				}

			},

		},
		onError : function(e) {
			// showButton();
			// addActive();
		},
		onSuccess : function(e) {

		}

	});

	

});
uploadAjax('#icon-form', '#progress-bar-icon', '#percent-icon');
function uploadAjax(form, progress_bar, percent) {

	var percentValue = "0";

	$(form).ajaxForm({
		// Do something before uploading
		beforeUpload : function() {
			result.empty();
			$('#result-icon').hide();
			$('#progress-icon').show();
			percentValue = "0%";
			$(progress_bar).css('width', percentValue);
			$(percent).html(percentValue);
			// console.log(percentValue);
		},

		// Do somthing while uploading
		uploadProgress : function(event, position, total, percentComplete) {
			$('#result-icon').hide();
			$('#progress-icon').show();
			percentValue = percentComplete + '%';
			$(progress_bar).css('width', percentValue);
			$(percent).html(percentValue);
			//console.log(percentValue);
		},

		// Do something while uploading file finish
		success : function() {
			percentValue = '100%';
			$(progress_bar).css('width', percentValue);
			$(percent).html(percentValue);
			//$('#result-icon').show();
			//$('#progress-icon').hide();
			// console.log(percentValue);
		},

		// Add response text to div #result when uploading complete
		complete : function(xhr) {
			//console.log((xhr.responseText));
			
			var result = $.parseJSON(xhr.responseText);
			if(result.status){
				$('#icon_view').attr('src',result.message.path);
			}else{
				view = '<div class="alert alert-danger" role="alert"><ul>';
				$.each(result.message, function(i,val){
					view += '<li>'+val+'</li>';
				});
				view += '</ul></div>';
				$('#result-icon').show();
				$('#result-icon').html(view);
			}
			
			$('#progress-icon').hide();
			percentValue = '0%';
			$(progress_bar).css('width', percentValue);
			$(percent).html(percentValue);
			// alert(percentValue);
		}
	});
}
var percentValue = "0";

$('#screenshot').ajaxForm({
	// Do something before uploading
	beforeUpload : function() {
		result.empty();
		$('#result-screen').hide();
		$('#progress-screen').show();
		percentValue = "0%";
		$('#progress-bar-screen').css('width', percentValue);
		$('#percent-screen').html(percentValue);
		// console.log(percentValue);
	},

	// Do somthing while uploading
	uploadProgress : function(event, position, total, percentComplete) {
		$('#result-screen').hide();
		$('#progress-screen').show();
		percentValue = percentComplete + '%';
		$('#progress-bar-screen').css('width', percentValue);
		$('#percent-screen').html(percentValue);
		//console.log(percentValue);
	},

	// Do something while uploading file finish
	success : function() {
		percentValue = '100%';
		$('#progress-bar-screen').css('width', percentValue);
		$('#percent-screen').html(percentValue);
		//$('#result-icon').show();
		//$('#progress-icon').hide();
		// console.log(percentValue);
	},

	// Add response text to div #result when uploading complete
	complete : function(xhr) {
		//console.log($.parseJSON(xhr.responseText));
		
		var result = $.parseJSON(xhr.responseText);
		if(result.status){
			$('#view_screenshot').append('<li><img src="'+result.message.path+'" ></li>');
		}else{
			view = '<div class="alert alert-danger" role="alert"><ul>';
			$.each(result.message, function(i,val){
				view += '<li>'+val+'</li>';
			});
			view += '</ul></div>';
			$('#result-screen').show();
			$('#result-screen').html(view);
		}
		
		$('#progress-screen').hide();
		percentValue = '0%';
		$('#progress-bar-screen').css('width', percentValue);
		$('#percent-screen').html(percentValue);
		
		$('#screen_app').wrap('<form>').closest('form').get(0).reset();
		$('#screen_app').unwrap();
		// alert(percentValue);
		
		if($('#view_screenshot li').size() >= 9){
			$('#doupscreen').remove();
		}
	}
});
